﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Reflection;
using Metaspec;

// Error and log callback example.

namespace CallbackExample
{
	class Program
	{
		static void Main(string[] args)
		{
			//creating project
			ICsProject project = ICsProjectFactory.create(project_namespace.pn_project_namespace);
			project.setBuildEntityModel(true); 

			//setting callbacks
			project.setErrorMessageCallback(project_error);
			project.setLogMessageCallback(project_log);

			//adding assemblies
			string assembly_path = Assembly.GetAssembly(typeof(object)).Location;
			byte[] assembly_buffer = File.ReadAllBytes(assembly_path);
			IExternalAssemblyModule module = IExternalAssemblyModuleFactory.create(assembly_buffer,assembly_path);
			project.addExternalAssemblyModules(new IExternalAssemblyModule[] { module }, true, loadAssemblyModuleCallback);

			//adding C# file
			string directory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"..\..");
			string path = Path.Combine(directory, "Error.cs");
			char[] buffer = File.ReadAllText(path).ToCharArray();
			ICsFile file = ICsFileFactory.create(buffer, path);
			project.addFiles(file);

			//parsing
			project.parse(true, false);

			//clear project callbacks
			project.setErrorMessageCallback(null);
			project.setLogMessageCallback(null);

			//creating solution
			ISolution solution = ISolutionFactory.create();
			solution.addProjects(project);

			//setting callbacks
			solution.setErrorMessageCallback(solution_error);
			solution.setLogMessageCallback(solution_log);

			//parsing
			solution.clear();
			solution.parse();
		}

		static IExternalAssemblyModule loadAssemblyModuleCallback ( IExternalAssemblyOwner externalAssemblyOwner, IExternalAssemblyModule module, string filename)
		{
			return null;
		}

		static void project_error(string message)
		{
			Console.WriteLine(message);
		}

		static void project_log(string message)
		{
			Console.WriteLine(message);
		}

		static void solution_error(string message)
		{
			Console.WriteLine(message);
		}

		static void solution_log(string message)
		{
			Console.WriteLine(message);
		}
	}
}
